package cn.toesbieya.jxc.controller.doc;

import cn.toesbieya.jxc.enumeration.DocStatusEnum;
import cn.toesbieya.jxc.model.entity.Notice;
import cn.toesbieya.jxc.model.vo.HonorWallVo;
import cn.toesbieya.jxc.model.vo.R;
import cn.toesbieya.jxc.model.vo.NoticeVo;
import cn.toesbieya.jxc.model.vo.UserVo;
import cn.toesbieya.jxc.model.vo.search.HonorWallSearch;
import cn.toesbieya.jxc.model.vo.search.NoticeSearch;
import cn.toesbieya.jxc.model.vo.update.DocStatusUpdate;
import cn.toesbieya.jxc.service.doc.NoticeService;
import cn.toesbieya.jxc.util.SessionUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping("doc/notice")
public class NoticeController {
    @Resource
    private NoticeService service;

    
    @GetMapping("getById")
    public R getById(@RequestParam String id) {
        if (StringUtils.isEmpty(id)) return R.fail("参数错误");
        NoticeVo vo = service.getById(id);
        return vo == null ? R.fail("获取单据信息失败") : R.success(vo);
    }

    @GetMapping("getSubById")
    public R getSubById(@RequestParam String id) {
        if (StringUtils.isEmpty(id)) return R.fail("参数错误");
        return R.success(service.getSubById(id));
    }

    @PostMapping("search")
    public R search(@RequestBody NoticeSearch vo) {
        return R.success(service.search(vo));
    }


    @PostMapping("add")
    public R add(@RequestBody NoticeVo vo) {
        if (StringUtils.isEmpty(vo.getName())) {
            return R.fail("参数错误");
        }
        UserVo user = SessionUtil.get();
        vo.setCname(user.getNickName());
        vo.setStatus(DocStatusEnum.DRAFT.getCode());

        return service.add(vo);
    }


    @PostMapping("update")
    public R update(@RequestBody NoticeVo vo) {
        String errMsg = validateUpdate(vo);
        if (errMsg != null) return R.fail(errMsg);
        return service.update(vo);
    }

    @PostMapping("commit")
    public R commit(@RequestBody NoticeVo vo) {
        boolean isFirst = StringUtils.isEmpty(vo.getId());
        String errMsg = null;
        if (!isFirst) errMsg = validateUpdate(vo);
        if (errMsg != null) return R.fail(errMsg);

        vo.setStatus(DocStatusEnum.WAIT_VERIFY.getCode());

        if (isFirst) {
            UserVo user = SessionUtil.get();

            vo.setCname(user.getNickName());
        }

        return service.commit(vo);
    }


    @PostMapping("pass")
    public R pass(@RequestBody DocStatusUpdate vo) {
        return service.pass(vo, SessionUtil.get());
    }


    @GetMapping("del")
    public R del(@RequestParam String id) {
        if (StringUtils.isEmpty(id)) return R.fail("参数错误");
        return service.del(id);
    }

    private String validateUpdate(Notice main) {
        if (StringUtils.isEmpty(main.getId())
                || StringUtils.isEmpty(main.getName())
                || StringUtils.isEmpty(main.getCname())
                || StringUtils.isEmpty(main.getCtime())
                || StringUtils.isEmpty(main.getStatus())
        ) return "参数错误";
        return null;
    }



}
